﻿using Microsoft.Extensions.Hosting;
using ThresholdAssistant.ConfigSdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// html内容解析工具类
    /// </summary>
    public static class HtmlUtil
    {
        /// <summary>
        /// 配置文件Key名称
        /// </summary>
        private static string ScanCodeFailureReasonKey = "";

        /// <summary>
        /// 
        /// </summary>
        private const string UnableToJoinChatRoomUnknownReason = "未知异常无法入群";

        /// <summary>
        /// 扫二维码失败的原因
        /// </summary>
        private static Dictionary<string, string> ScanCodeFailureReason = new Dictionary<string, string>
        {
            {"10000", UnableToJoinChatRoomUnknownReason },
            {"10001", "该二维码分享者已离开群聊，无法加入" },
            {"10002", "因二维码分享者处于非正常可登录状态，无法加入群聊" },
            {"10003", "二维码已过期" },
            {"10004", "群聊人数超过100人，只可通过群成员邀请进群" },
            {"10005", "该群已开启进群验证只可通过邀请进群" },
            {"10006", "系统繁忙" },
            {"10007", "该群已不存在" },
            {"10008", "系统错误" }
        };

        /// <summary>
        /// 扫二维码失败的原因
        /// </summary>
        public static Dictionary<string, string> GetScanCodeFailureReasonDic()
        {
            var confDic = ConfigUtil.GetValue<Dictionary<string, string>>(ScanCodeFailureReasonKey);
            if (confDic != null && confDic.Any())
            {
                foreach (var scanCodeFailureReasoItem in ScanCodeFailureReason)
                {
                    if (!confDic.ContainsKey(scanCodeFailureReasoItem.Key))
                    {
                        confDic.Add(scanCodeFailureReasoItem.Key, scanCodeFailureReasoItem.Value);
                    }
                }
            }
            else
            {
                confDic = ScanCodeFailureReason;
            }
            return confDic;
        }

        /// <summary>
        /// 注入错误原因
        /// </summary>
        /// <param name="key"></param>
        public static IHostBuilder UseFailureReason(this IHostBuilder builder, string key = "ScanCodeFailureReasonDic")
        {
            ScanCodeFailureReasonKey = key;
            return builder;
        }

        /// <summary>
        /// 获取html内容中的文本内容
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public static string ToText(string htmlContent)
        {
            try
            {
                htmlContent = Regex.Replace(htmlContent, @"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                htmlContent = Regex.Replace(htmlContent, @"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                htmlContent = Regex.Replace(htmlContent, @"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                htmlContent = Regex.Replace(htmlContent, @"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                htmlContent = Regex.Replace(htmlContent, @"(<[^>]+?>)| ", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                return Regex.Replace(htmlContent, @"(\s)+", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取扫码返回的原因，未来做成可以配置的方式
        /// </summary>
        /// <param name="text">微信扫码后展示的失败原因</param>
        /// <returns>失败代码</returns>
        public static KeyValuePair<string, string> GetFailureCode(string text)
        {
            var confDic = GetScanCodeFailureReasonDic();
            if (confDic.ContainsValue(text))
            {
                return confDic.FirstOrDefault(s => s.Value == text);
            }
            return confDic.FirstOrDefault(s => s.Value == UnableToJoinChatRoomUnknownReason);
        }
    }
}
